<?php

namespace app\admin\controller;

use app\admin\model\Admin;
use app\admin\model\User;
use app\common\controller\Backend;
use app\common\model\Attachment;
use fast\Date;
use think\Db;

/**
 * 控制台
 *
 * @icon   fa fa-dashboard
 * @remark 用于展示当前系统中的统计数据、统计报表及重要实时数据
 */
class Dashboard extends Backend
{

    /**
     * 查看
     */
    public function index()
    {

        // halt(getLast7Months());

        $months = array_reverse(getLast7Months());
        $customer_add = [];
        foreach ($months as $month =>$val){
            $customer_add[$month] = \app\admin\model\goods\Customer::where('createtime','between',$val)->count();
        }
        $user_add = [];
        foreach ($months as $month =>$val){
            $user_add[$month] = \app\admin\model\User::where('level_update_time','between',$val)->count();
        }

        $customer_add_compare =$user_add_compare= [];
        $office_list = \app\admin\model\branchoffice\Office::column('id','name');

        foreach ($office_list as $key=> $office_id){
            $customer_add_compare[$key] = \app\admin\model\goods\Customer::where('office_id',$office_id)->whereTime('createtime','month')->count();
        }


        foreach ($office_list as $key=> $office_id){
            $user_add_compare[$key] = \app\admin\model\User::where('belongto_office_id',$office_id)->whereTime('level_update_time','month')->count();
        }

        foreach ($months as $month =>$val){
            $goods_add[$month] = \app\branchoffice\model\Goods::where('createtime','between',$val)->count();
        }

        foreach ($office_list as $key=> $office_id){
            $goods_add_compare[$key] = \app\branchoffice\model\Goods::where('office_id',$office_id)->whereTime('createtime','month')->count();
        }

        $this->view->assign([
            'month_goods_total'  => \app\admin\model\Goods::whereTime('createtime','month')->count(),
            'month_customer_total'  => \app\admin\model\goods\Customer::whereTime('createtime','month')->count(),
            'month_vip_add'       => \app\admin\model\User::whereTime('level_update_time','month')->count(),
            'vip_total' => \app\admin\model\User::where('level','>',0)->count(),
            'customer_add'          => $customer_add,
            'user_add'        => $user_add,
            'goods_add'        => $goods_add,
            'customer_add_compare'       => $customer_add_compare,
            'user_add_compare'       => $user_add_compare,
            'goods_add_compare'        => $goods_add_compare,
        ]);

        return $this->view->fetch();


    }

}
